package treetagger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class TreeTaggerNormalizer implements Normalizer {

    private String treeTaggerBin;
    /**
     * Les cat�gories de mots outils
     */
    private static ArrayList<String> stopCats;

    static {
        stopCats = new ArrayList<String>();
        stopCats.add("PUN");
        stopCats.add("PRO");
        stopCats.add("PRP");
        stopCats.add("DET");
        stopCats.add("SENT");
        stopCats.add("KON");
    }

    public TreeTaggerNormalizer(String treeTaggerBin) {
        this.treeTaggerBin = treeTaggerBin;
    }

    @Override
    public ArrayList<String> normalize(String fileName) {

        String command = treeTaggerBin + " " + fileName;
        final ArrayList<String> lemmas = new ArrayList<String>();

        try {
            final Process proc = Runtime.getRuntime().exec(command);
            // Consommation de la sortie standard de l'application externe dans
            // un Thread separe
            Thread t = new Thread() {
                public void run() {
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(proc.getInputStream()));
                        String line = "";
                        String[] fields;
                        String lemma;
                        boolean keepIt;
                        try {
                            while ((line = reader.readLine()) != null) {
                                // Traitement du flux de sortie de l'application
                                // si besoin est
                                fields = line.split("\t");
                                if (fields.length == 3) {
                                    lemma = fields[2];
                                    // si le lemme est "unknown", on remplace
                                    // par la forme de surface
                                    if (lemma.equals("<unknown>") || lemma.equals("@card@")) {
                                        lemmas.add(fields[0]);
                                    } else {
                                        lemmas.add(lemma);
                                    }
                                }
                            }
                        } finally {
                            reader.close();
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            };

            t.start();


            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
        }

        return lemmas;
    }

    public static void main(String[] args) {
        String word = "/home/xtannier/tmp/texte.txt";
        Normalizer stemmer = new TreeTaggerNormalizer("/home/xtannier/tools/TreeTagger/cmd/tree-tagger-french");

        System.out.println(stemmer.normalize(word));

    }
}
